package com.lurencun.android.topicbank.presist;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DataHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "Exam.db";
	private static final int DATABASE_VERSION = 3;
	
	private Dao<Profile, Long> profileDao = null;
	
	private Dao<Paper, Long> paperDao = null;
	private Dao<Topic, Long> topicDao = null;
	private Dao<Answer, Long> answerDao = null;
	private Dao<Solution, Long> solutionDao = null;
	private Dao<Technique, Long> techniqueDao = null;
	private Dao<Practice, Long> practiceDao = null;
	private Dao<FailureTopic, Long> failureDao = null;
	public DataHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Profile.class);
			TableUtils.createTable(connectionSource, Paper.class);
			TableUtils.createTable(connectionSource, Topic.class);
			TableUtils.createTable(connectionSource, Answer.class);
			TableUtils.createTable(connectionSource, Solution.class);
			TableUtils.createTable(connectionSource, Technique.class);
			TableUtils.createTable(connectionSource, Practice.class);
			TableUtils.createTable(connectionSource, FailureTopic.class);
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "create datebase error", e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int arg2, int arg3) {
		try {
			TableUtils.dropTable(connectionSource, Profile.class, true);
			TableUtils.dropTable(connectionSource, Paper.class, true);
			TableUtils.dropTable(connectionSource, Topic.class, true);
			TableUtils.dropTable(connectionSource, Answer.class, true);
			TableUtils.dropTable(connectionSource, Solution.class, true);
			TableUtils.dropTable(connectionSource, Technique.class, true);
			TableUtils.dropTable(connectionSource, Practice.class, true);
			TableUtils.dropTable(connectionSource, FailureTopic.class, true);
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "upgrade database error", e);
		}
	}

	@Override
	public void close() {
		super.close();
		profileDao = null;
		paperDao = null;
		topicDao = null;
		answerDao = null;
		solutionDao = null;
		techniqueDao = null;
		practiceDao = null;
		failureDao = null;
	}
	
	public Dao<Profile, Long> getProfileDao() throws SQLException {
		if (profileDao == null) {
			profileDao = getDao(Profile.class);
		}
		return profileDao;
	}
	
	public Dao<Paper, Long> getPaperDao() throws SQLException {
		if (paperDao == null) {
			paperDao = getDao(Paper.class);
		}
		return paperDao;
	}
	
	public Dao<Topic, Long> getTopicDao() throws SQLException {
		if (topicDao == null) {
			topicDao = getDao(Topic.class);
		}
		return topicDao;
	}

	public Dao<Answer, Long> getAnswerDao() throws SQLException {
		if (answerDao == null) {
			answerDao = getDao(Answer.class);
		}
		return answerDao;
	}

	public Dao<Solution, Long> getSolutionDao() throws SQLException {
		if (solutionDao == null) {
			solutionDao = getDao(Solution.class);
		}
		return solutionDao;
	}

	public Dao<Technique, Long> getTechniqueDao() throws SQLException {
		if (techniqueDao == null) {
			techniqueDao = getDao(Technique.class);
		}
		return techniqueDao;
	}

	public Dao<Practice, Long> getPracticeDao() throws SQLException {
		if (practiceDao == null) {
			practiceDao = getDao(Practice.class);
		}
		return practiceDao;
	}

	public Dao<FailureTopic, Long> getFailureDao() throws SQLException {
		if (failureDao == null) {
			failureDao = getDao(FailureTopic.class);
		}
		return failureDao;
	}
	
}